﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using snat.proxy.items;

namespace snat.proxy.sqlapi {
    public static class MessageQuery {

        public static ListProxy<Message> All(bool isGreedy = true) {
            return Query.SQLQuery<snat.proxy.items.Message>(
                String.Join(" ", Query.SELECT, Query.ALL, Query.FROM, 
                                "(", Query.SELECT, Query.ALL, Query.FROM, Query.MessageProperties, 
                                        Query.NATURAL_JOIN, Query.MessageSend, ")",  
                                        Query.AS, "dummy", 
                                Query.NATURAL_JOIN, Query.Entity, 
                                Query.ORDER_BY, Query.TimeStamp), isGreedy);
        }

        public static ListProxy<Message> SentBy(int id, bool isGreedy = false) {
            return Query.SQLQuery<Message>(String.Join(" ",
                    Query.SELECT, Query.ALL, Query.FROM, "(", Query.SELECT, Query.ALL, Query.FROM, Query.MessageSend, 
                        Query.NATURAL_JOIN, Query.MessageProperties, ")", Query.AS, "dummy",
                        Query.NATURAL_JOIN, Query.Entity,
                    Query.WHERE, Query.EntityID, Query.EQUALS, id), isGreedy);
        }

        public static ListProxy<Message> Sent(bool isGreedy = false) {
            return Query.SQLQuery<Message>(String.Join(" ",
                    Query.SELECT, Query.ALL, Query.FROM, "(", Query.SELECT, Query.ALL, Query.FROM, Query.MessageSend,
                        Query.NATURAL_JOIN, Query.MessageProperties, ")", Query.AS, "dummy",
                        Query.NATURAL_JOIN, Query.Entity), isGreedy);
        }

        public static ListProxy<Message> ReceivedBy(int id, bool isGreedy = false) {
            return Query.SQLQuery<Message>(String.Join(" ",
                    Query.SELECT, Query.ALL, Query.FROM, "(", Query.SELECT, Query.ALL, Query.FROM, Query.MessageReceive,
                        Query.NATURAL_JOIN, Query.MessageProperties, ")", Query.AS, "dummy",
                        Query.NATURAL_JOIN, Query.Entity,
                    Query.WHERE, Query.EntityID, Query.EQUALS, id), isGreedy);
        }

        public static ListProxy<Message> Received(bool isGreedy = false) {
            return Query.SQLQuery<Message>(String.Join(" ",
                    Query.SELECT, Query.ALL, Query.FROM, "(", Query.SELECT, Query.ALL, Query.FROM, Query.MessageReceive,
                        Query.NATURAL_JOIN, Query.MessageProperties, ")", Query.AS, "dummy",
                        Query.NATURAL_JOIN, Query.Entity), isGreedy);
        }

        public static Message FirstMessage(bool isGreedy = true) {
            return Query.ConstantQuery<Message>(String.Join(" ", 
                        Query.SELECT, Query.ALL, Query.FROM, 
                            "(", Query.SELECT, Query.ALL, Query.FROM, Query.MessageProperties, 
                                    Query.NATURAL_JOIN, Query.MessageSend, ")",  
                                    Query.AS, "dummy", 
                            Query.NATURAL_JOIN, Query.Entity, 
                            Query.ORDER_BY, Query.TimeStamp, Query.LIMIT, "1", ",", "1"), isGreedy);
        }

        public static Message LastMessage(bool isGreedy = true) {
            return Query.ConstantQuery<Message>(String.Join(" ",
                        Query.SELECT, Query.ALL, Query.FROM,
                            "(", Query.SELECT, Query.ALL, Query.FROM, Query.MessageProperties,
                                    Query.NATURAL_JOIN, Query.MessageSend, ")",
                                    Query.AS, "dummy",
                            Query.NATURAL_JOIN, Query.Entity,
                            Query.ORDER_BY, Query.TimeStamp, Query.DESC, Query.LIMIT, "1", ",", "1"), isGreedy);
        }
    }
}
